package Client.Manager
{
	
	/**
	 * class		EventMgr
	 * @desc		used to manage the events in game's event turn
	 * @author		Aqua
	 * @date		2011-9-7
	 */
	public class EventMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function					instance getter
		 * @desc					a private function used to enforce singlton
		 * @return	EventMgr		singleton instance of this class
		 *************************************************************************/
		public static function get instance():EventMgr
		{
			if(null == _instance)
			{
				_instance = new EventMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function			singleton
		 * @desc			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function			EventMgr
		 * @desc			constructor of class
		 * @param	s		a private function used to enforce singlton
		 *************************************************************************/
		public function EventMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function			setAllEvents
		 * @desc			add events from XML data
		 * @param	xml		XML data that contains event data
		 *************************************************************************/
		public function setAllEvents(xml:XML):void
		{
			var evtList	:XMLList;
			var item	:XML;
			
			_eventPack = [[], [], []];
			
			for each(item in xml.pack_1.event)
			{
				_eventPack[0].push(item.event);
			}
			for each(item in xml.pack_2.event)
			{
				_eventPack[1].push(item.event);
			}
			for each(item in xml.pack_3.event)
			{
				_eventPack[2].push(item.event);
			}
		}
		
		/**************************************************************************
		 * function			refreshEventPack
		 * @desc			create a new pack of events with random order
		 *************************************************************************/
		public function refreshEventPack():void
		{
			var newPack:Array;
			var i:int;
			
			if (null == _eventPack)
			{
				return;
			}
			
			newPack = [[], [], []];
			i = 0;
			
			while (i < 3)
			{
				randomOrder(_eventPack[i], newPack[i]);
				i ++;
			}
			
			// set a random order for all elements in an array
			function randomOrder(oldArr:Array, newArr:Array):void
			{
				var len		:int;
				var index	:int;
				
				len = oldArr.length;
				
				while (len >= 0)
				{
					index = int(Math.random() * len);
					newArr.push(oldArr.splice(index, 1)[0]);
					len --;
				}
			}
			
			_eventPack = newPack;
			_currentEvents = [];
		}
		
		/**************************************************************************
		 * function			triggerNewEvents
		 * @desc			trigger three events those would occur in current turn
		 *************************************************************************/
		public function triggerNewEvents():void
		{
			var i		:int;
			var oldEvt	:String;
			var newEvt	:String;
			
			i = 0;
			
			while (i < 3)
			{
				oldEvt = _currentEvents[i];
				newEvt = _eventPack[i].splice(0, 1)[0];
				_currentEvents[i] = newEvt;
				_eventPack[i].push(oldEvt);
				i ++;
			}
			
			handleEvents();
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		/**************************************************************************
		 * function			handleEvents
		 * @desc			handle the events those would occur in current turn
		 *************************************************************************/
		private function handleEvents():void
		{
			var evt	:String;
			var i	:int;
			
			i = 0;
			
			while (i < 3)
			{
				evt = _currentEvents[i];
				
				switch(evt)
				{
				case "evt_0":		// 征召士兵
					break;
				default:
					break;
				}
			}
		}
		
		
		private static var _instance:EventMgr;
		
		private var _eventPack		:Array;
		private var _currentEvents	:Array;
	}
	
}